package org.dromara.oce.controller.inner;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.oce.constant.OceEventTypeEnums;
import org.dromara.oce.domain.bo.OceanEngineOrderReportBo;
import org.dromara.oce.domain.bo.OceanEngineReportBo;
import org.dromara.oce.domain.dto.OceanEngineReport;
import org.dromara.oce.service.remote.OceanEngineRemoteService;
import org.dromara.oce.utils.DateTimeUtil;
import org.dromara.oce.utils.PlatformParamTransUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 巨量上报
 * @desc:
 * @author: hhl
 * @date：2025/2/21 16:38
 * @module 对内接口
 */
@SaIgnore
@RestController
@RequestMapping("/oce")
@RequiredArgsConstructor
@Slf4j
public class OceReportController {

    private final OceanEngineRemoteService oceanEngineRemoteService;

    /**
     * 激活上报
     */
    @RepeatSubmit()
    @PostMapping("/activeReport")
    public R<Boolean> activeReport(@Validated @RequestBody OceanEngineReportBo bo) {
        OceanEngineReport req = new OceanEngineReport();
        req.setEventTypeEnums(OceEventTypeEnums.ACTIVE);
        req.setTimestamp(DateTimeUtil.getTimestampMilli(bo.getCreateTime()));
        req.setCallback(bo.getClickId());
        req.setConvTime(DateTimeUtil.getTimestampSecond(bo.getConvTime()));
        req.setOuterEventId(bo.getUserId() + "");
        req.setPlatform(bo.getDevicePlatform());
        boolean flag = oceanEngineRemoteService.reportBack(req);
        if(flag){
            return R.ok("激活上报成功");
        }
        return R.fail("激活上报失败");
    }

    /**
     * ROI上报
     */
    @RepeatSubmit()
    @PostMapping("/roiReport")
    public R<Boolean> roiReport(@Validated @RequestBody OceanEngineReportBo bo) {
        OceanEngineReport req = new OceanEngineReport();
        req.setEventTypeEnums(OceEventTypeEnums.LT_ROI);
        req.setTimestamp(DateTimeUtil.getTimestampMilli(bo.getCreateTime()));
        req.setCallback(bo.getClickId());
        req.setConvTime(DateTimeUtil.getTimestampSecond(bo.getConvTime()));
        req.setOuterEventId(bo.getUserId() + "");
        req.setPlatform(bo.getDevicePlatform());
        boolean flag = oceanEngineRemoteService.reportBack(req);
        if(flag){
            return R.ok("ROI上报成功");
        }
        return R.fail("ROI上报失败");
    }

    /**
     * 订单上报
     */
    @RepeatSubmit()
    @PostMapping("/orderReport")
    public R<Boolean> orderReport(@Validated @RequestBody OceanEngineOrderReportBo bo) {
        boolean flag = false;
        String clickId = bo.getClickId();
        if (ObjectUtils.isEmpty(clickId) || "__CLICKID__".equals(clickId)) {
            log.warn("订单号：{}，没有找到广告点击记录，不进行上报", bo.getOutTradeNo());
            return R.fail("订单号："+bo.getOutTradeNo()+"没有找到广告点击记录，不进行上报");
        }
        if (PlatformParamTransUtil.isWechat(bo.getOrderPlatform())) {
            //如果是微信,支付后回传事件：active_pay
            OceanEngineReport req = new OceanEngineReport();
            req.setEventTypeEnums(OceEventTypeEnums.ACTIVE_PAY);
            req.setTimestamp(DateTimeUtil.getTimestampMilli(bo.getCreateTime()));
            req.setCallback(clickId);
            req.setPlatform(bo.getDevicePlatform());
            req.setPayAmount(bo.getTotalAmount());
            req.setOrderId(bo.getOutTradeNo());
            req.setConvTime(DateTimeUtil.getTimestampSecond(bo.getConvTime()));
            flag = oceanEngineRemoteService.reportBack(req);
        }else if (PlatformParamTransUtil.isDouYin(bo.getOrderPlatform())) {
            //如果是抖音，支付后需要去回传三个事件：active_pay  purchase_roi supply_active_pay
            OceanEngineReport req = new OceanEngineReport();
            req.setEventTypeEnums(OceEventTypeEnums.ACTIVE_PAY);
            req.setTimestamp(DateTimeUtil.getTimestampMilli(bo.getCreateTime()));
            req.setCallback(clickId);
            req.setPlatform(bo.getDevicePlatform());
            req.setPayAmount(bo.getTotalAmount());
            req.setOrderId(bo.getOutTradeNo());
            req.setConvTime(DateTimeUtil.getTimestampSecond(bo.getConvTime()));
            flag = oceanEngineRemoteService.reportBack(req);
            OceanEngineReport req2 = new OceanEngineReport();
            BeanUtil.copyProperties(req, req2);
            req2.setEventTypeEnums(OceEventTypeEnums.PURCHASE_ROI);
            flag = oceanEngineRemoteService.reportBack(req2);
            OceanEngineReport req3 = new OceanEngineReport();
            BeanUtil.copyProperties(req, req3);
            req3.setEventTypeEnums(OceEventTypeEnums.SUPPLY_ACTIVE_PAY);
            flag = oceanEngineRemoteService.reportBack(req3);
        }
        return R.ok(flag);
    }

}
